<?php

namespace app\website\controller;

use app\AdminRest;
use app\website\model\StarBanner;
use app\website\model\StarCate;
use app\website\model\StarChoose;
use app\website\model\StarList;

use think\App;

class Star extends AdminRest
{


    protected $model;

    protected $cate_model;

    protected $choose_model;

    protected $choose_banner;

    // 继承 验证用户登陆
    public function __construct ( App $app )
    {
        parent::__construct( $app );

        $this->model = new StarList();

        $this->cate_model = new StarCate();

        $this->choose_model = new StarChoose();

        $this->banner_model = new StarBanner();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 14:56
     * @功能说明:分类列表
     */
    public function cateList(){


        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['modular_id','=',$input['modular_id']];

        $data = $this->cate_model->cateList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:00
     * @功能说明:添加分类
     */
    public function cateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->cate_model->cateAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:36
     * @功能说明:分类详情
     */
    public function cateInfo(){

        $input = $this->_param;

        $dis['id'] = $input['id'];

        $data = $this->cate_model->cateInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 17:03
     * @功能说明:一级分类
     */
    public function oneCateSelect(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['pid','=',0];

        $dis[] = ['status','=',1];

        $dis[] = ['modular_id','=',$input['modular_id']];

        $data = $this->cate_model->where($dis)->field('id,title')->order('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 17:17
     * @功能说明:下拉分类
     */
    public function cateSelect(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['modular_id','=',$input['modular_id']];

        $data = $this->cate_model->where($dis)->field('id,pid,title')->order('top desc,id desc')->select()->toArray();

        $data = $this->cate_model->getTree($data,0);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:00
     * @功能说明:编辑分类
     */
    public function cateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $info = $this->cate_model->cateInfo($dis);

        if($info['pid']==0&&$info['status']==1&&$input['status']==0){

            $this->cate_model->cateUpdate(['pid'=>$info['id']],['status'=>0]);

        }

        if($info['pid']==0&&!empty($input['pid'])){

            $this->errorMsg('一级分类没有父级分类');
        }


        $data = $this->cate_model->cateUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:05
     * @功能说明:banner列表
     */
    public function bannerList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['modular_id','=',$input['modular_id']];


        $data = $this->banner_model->bannerList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:38
     * @功能说明:banner详情
     */
    public function bannerInfo(){

        $input = $this->_param;

        $dis['id'] = $input['id'];

        $data = $this->banner_model->bannerInfo($dis);

        $cate = $this->cate_model->where(['id'=>$data['cate_id']])->value('pid');

        if(!empty($cate)){

            $data['cate_id'] = [$cate,$data['cate_id']];

        }else{

            $data['cate_id'] = [$data['cate_id']] ;
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:05
     * @功能说明:banner列表
     */
    public function bannerAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['cate_id'] = array_pop($input['cate_id']);

        $data = $this->banner_model->bannerAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:00
     * @功能说明:编辑分类
     */
    public function bannerUpdate(){

        $input = $this->_input;

        $dis['id'] = $input['id'];

        if(isset( $input['cate_id'])){

            $input['cate_id'] = array_pop($input['cate_id']);
        }

        $data = $this->banner_model->bannerUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:12
     * @功能说明:列表
     */
    public function chooseList(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['type','=',$input['type']];

        $dis[] = ['modular_id','=',$input['modular_id']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $data = $this->choose_model->chooseList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 17:18
     * @功能说明:
     */
    public function chooseSelect(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['type','=',$input['type']];

        $dis[] = ['modular_id','=',$input['modular_id']];


        $data  = $this->choose_model->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-24 17:22
     * @功能说明:
     */
    public function allChooseSelect(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['modular_id','=',$input['modular_id']];

        $list = [1,2,3,4];

        foreach ($list as $k=>$value){

//            $dis[] = ['type','=',$value];

            $arr[$k]['type'] = $value;

            $arr[$k]['list'] = $this->choose_model->where($dis)->where(['type'=>$value])->order('top desc,id desc')->select()->toArray();;

        }

        return $this->success($arr);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:12
     * @功能说明:列表
     */
    public function chooseAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->choose_model->chooseAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:39
     * @功能说明:详情
     */
    public function chooseInfo(){

        $input = $this->_param;

        $dis['id'] = $input['id'];

        $data = $this->choose_model->chooseInfo($dis);

        $cate = $this->cate_model->where(['id'=>$data['cate_id']])->value('pid');

        if(!empty($cate)){

            $data['cate_id'] = [$cate,$data['cate_id']];

        }else{

            $data['cate_id'] = [$data['cate_id']] ;
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:27
     * @功能说明:修改
     */
    public function chooseUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->choose_model->chooseUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:40
     * @功能说明:列表
     */
    public function starList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['modular_id','=',$input['modular_id']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->model->startList($dis,$input['limit']);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-24 17:42
     * @功能说明:
     */

    public function starSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['modular_id','=',$input['modular_id']];

        if(!empty($input['id'])){

            $dis[] = ['id','<>',$input['id']];
        }

        $data = $this->model->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:43
     * @功能说明:添加
     */
    public function starAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->startAdd($input);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:45
     * @功能说明:详情
     */
    public function starInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->startInfo($dis);

        $data = $this->model->returnInfo($data);

        $cate = $this->cate_model->where(['id'=>$data['cate_id']])->value('pid');

        if(!empty($cate)){

            $data['cate_id'] = [$cate,$data['cate_id']];

        }else{

            $data['cate_id'] = [$data['cate_id']] ;
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:46
     * @功能说明:编辑
     */
    public function starUpdate(){

        $input = $this->_input;

        $dis['id'] = $input['id'];

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->startUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:47
     * @功能说明:上下架
     */
    public function starStatusUpdate(){

        $input = $this->_input;

        $dis['id'] = $input['id'];

        $res = $this->model->where($dis)->update(['status'=>$input['status']]);

        return $this->success($res);

    }



}
